package com.zhihu_lvliangi_android.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Environment;

import com.android.activity.AndroidApplication;
import com.android.file.FileUtils;
import com.android.volley.Response;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.core.BitmapSize;
import com.zhihu_lvliangi_android.util.Constant;

/**
 * 
 * 
 * @author azuryleaves
 * @version 1.0
 * 
 */
public class MobileApplication extends AndroidApplication {

	public static final String TAG = "zhuihui_lvliang";

	public ClientTask clientTask;
	private static MobileApplication mobileApplication;
	private static ArrayList<Response.Listener<?>> listeners = new ArrayList<Response.Listener<?>>();
	public static SharedPreferences preferences;
	public NetReceiver rev = new NetReceiver(); // 监控网络状态（是否在网）

	@Override
	public void onCreate() {
		AppRootPath = TAG;
		taskOperate = clientTask = new ClientTask(getApplicationContext());
		notDebug = false;
		super.onCreate();
		mobileApplication = this;
		IntentFilter filter = new IntentFilter();
		filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		registerReceiver(rev, filter); // 注册监控网络状态的广播
		preferences = getSharedPreferences(TAG, Context.MODE_PRIVATE);
//		FileUtils.createMoreDocuments(TAG);
		if (!FileUtils.isFileExist(Constant.SD_PATH)) {
			FileUtils.createMoreDocuments(Constant.SD_LOG_PATH);
			FileUtils.createMoreDocuments(Constant.SD_CACHE_PATH);
			FileUtils.createMoreDocuments(Constant.SD_PIC_PATH);
		}
		
		
	}

	public static synchronized MobileApplication getInstance() {
		return mobileApplication;
	}

	public Response.Listener<?> getResponseListenerByName(String name) {
		Response.Listener<?> listener = null;
		for (Response.Listener<?> rl : listeners) {
			if (name.startsWith(rl.getClass().getName())) {
				listener = rl;
				break;
			}
		}
		return listener;
	}

	public void exitApp(Context context) {
		try {
			MobileApplication.getInstance().unregisterReceiver(MobileApplication.getInstance().rev);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		super.exitApp(context);
	}

	public BitmapDisplayConfig initBitMapConfig(int LoadingResId,int failedLoadId) {
		BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
		BitmapSize bitmapMaxSize = new BitmapSize(120, 120);
		displayConfig.setBitmapMaxSize(bitmapMaxSize);
		displayConfig.setLoadingDrawable(this.getResources().getDrawable(
				LoadingResId));
		displayConfig.setLoadFailedDrawable(getResources().getDrawable(failedLoadId));
	
		return displayConfig;
	}
	
	public boolean isSDCardMounted(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}

}
